/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.server;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
 *
 * @author mark
 */
public class SqlConnection {
    
    private Main main;
    private final String URL = "jdbc:sqlserver://localhost;databaseName=Navision;integratedSecurity=true;";
    private Connection connection;
    private Statement statement;
    private String query;
    private ResultSet rs;
    //private Log log = new Log(main);
    
    public SqlConnection( Main main ){
        this.main = main;
        loadDriver();
        connect();
    }
    
    //Methodes
    //Laden van Database Driver
    private void loadDriver() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            main.getMainScreen().addToLog("Database driver loaded");
            //log.addToLog("Database driver loaded");
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(main.getMainScreen(),"De server kan de database driver niet vinden","Driver niet gevonden",JOptionPane.WARNING_MESSAGE);
            ex.printStackTrace();
            main.getMainScreen().addToLog("Database driver couldn't load");
            //log.addToLog("Database driver couldn't load");
        }

    }
    //Connectie Database
    private void connect() {
        try {
            connection = DriverManager.getConnection(URL);
            statement = connection.createStatement();
            main.getMainScreen().addToLog("Database connection succes");
            //log.addToLog("Database connection succes");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(main.getMainScreen(),"De server kan geen verbinding maken met de database","Databaseverbinding mislukt",JOptionPane.WARNING_MESSAGE);
            ex.printStackTrace();
            main.getMainScreen().addToLog("Database connection failed");
            //log.addToLog("Database connection failed");
        }
    }
    
    public ResultSet executeSelectQuery( String query ){
        this.query = query;
        ResultSet rs = null;
        if (query.startsWith("SELECT")){
            try {
                rs = statement.executeQuery(query);
            }   catch(SQLException ex){   
                JOptionPane.showMessageDialog(main.getMainScreen(),"De server kan de query niet uitvoeren","Uitvoeren query mislukt",JOptionPane.WARNING_MESSAGE);
                main.getMainScreen().addToLog("Cant execute query:" + query );
                //log.addToLog("Cant execute query:" + query );
            }
        } else if (query.startsWith("UPDATE") || query.startsWith("INSERT") || query.startsWith("DELETE") )  {
            System.out.println("Query dousn't start with SELECT");
            try {
                statement.executeUpdate(query);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(main.getMainScreen(),"De server kan de query niet uitvoeren","Uitvoeren query mislukt",JOptionPane.WARNING_MESSAGE);
                ex.printStackTrace();
                main.getMainScreen().addToLog("Cant execute query:" + query );
                //log.addToLog("Cant execute query:" + query );
            }
        }
        return rs;
    }
}
